<template>
  <div class="home">
    <el-container class="home-contaner">
      <!-- 头部 -->
      <el-header>
        <div>
          <img src="../assets/img/heima.png" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- container -->
      <el-container>
        <!-- 侧边栏 -->
        <!-- 侧边栏的宽根据有没有折叠的效果来动态展示 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!--菜单栏的折叠按钮 是否只一个子菜单显示 开启折叠 为一级开启路由模式 加高亮效果动态绑定-->
          <div class="toggle-button" @click="toggle">|||</div>
          <el-menu unique-opened background-color="#545c64" text-color="#fff" active-text-color="#409eff" :collapse="isCollapse" :collapse-transition="false" router :default-active="isacitvePath">
            <!-- 注意：index来确定唯一值。确保每次点击的是不同的 index绑定的是字符串类型，而item.id是数值，得转换 -->
            <el-submenu :index="item.id + ''" :key="item.id" v-for="item in menuList">
              <!-- 一级分类 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级分类 -->
              <!-- 点二级菜单都跳到不同的路由地址 由于element-ui的路由跳转的是根据id地址 我们根据index属来跳转   -->
              <!-- 点击二级菜单分别给高亮 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 保存激活的连接
      isacitvePath: "",
      // 是否折叠
      isCollapse: false,
      menuList: [],
      iconsObj: {
        "125": "iconfont icon-album",
        "103": "iconfont icon-hot",
        "101": "iconfont icon-invisible",
        "102": "iconfont icon-upload",
        "145": "iconfont icon-settings_outlined"
      }
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push("/Login")
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus")
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error("获取数据失败")
      this.menuList = res.data
    },
    // 点击按钮实现菜单栏的折叠与展开
    toggle() {
      this.isCollapse = !this.isCollapse
    },
    // 保存连接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activepath", activePath)
      this.isacitvePath = activePath
    }
  },
  created() {
    this.getMenuList()
    this.isacitvePath = window.sessionStorage.getItem("activepath")
  }
}
</script>
<style lang="less" scoped>
.toggle-button {
  color: white;
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.home {
  height: 100%;
}
.home-contaner {
  height: 100%;
}
.el-header {
  background-color: #272c2e;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    align-items: center;
    display: flex;
    > span {
      margin-left: 15px;
    }
  }
}
i {
  margin-right: 8px;
}
.el-aside {
  background-color: #313743;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #e9edf1;
}
</style>
